package cn.csust.net2.manager.client.chat;

import java.util.Date;

import cn.csust.net2.manager.shared.po.Message;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.BeanService;
import cn.csust.net2.manager.shared.service.BeanServiceAsync;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonGroup;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RichTextArea;

public class ChatTab extends  TabItem {


	private ButtonGroup bottom = new ButtonGroup(2);

	private BorderLayoutData centerData;
	private BorderLayoutData southDate;
	
	private StringBuffer chatContent;
	//聊天内容输入框
	private HtmlEditor htmlEditor;
	//聊天内容显示框
	private RichTextArea htmlShower;
	
	//表示是否为活动状态，即是否显示
	private boolean isActive = true;

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	protected User sender;
	protected User receiver;

	public ChatTab(User sender, User receiver) {
		this.setSender(sender);
		this.setReceiver(receiver);
		initLayout();
	}

	private void initLayout() {
		
		chatContent = new StringBuffer("");
		
		this.setStyleAttribute("padding", "5px");
		this.setClosable(true);
		this.setText(receiver.getName());
		this.setLayout(new BorderLayout());

		centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setCollapsible(true);
		centerData.setMargins(new Margins(0));

		southDate = new BorderLayoutData(LayoutRegion.SOUTH);
		southDate.setCollapsible(true);
		southDate.setMargins(new Margins(0));

		
		htmlShower = new RichTextArea();
		htmlShower.setEnabled(false);
		htmlShower.setFocus(false);

		FormPanel panel = new FormPanel();
		panel.setBorders(false);
		panel.setBodyBorder(false);
		panel.setLabelWidth(55);
		panel.setPadding(5);
		panel.setHeaderVisible(false);

		htmlEditor = new HtmlEditor();
		htmlEditor.setHideLabel(true);
		panel.add(htmlEditor, new FormData("100%"));

		Button send = new Button("发送");
		send.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				
				chatContent.append(sender.getName() + " : " + htmlEditor.getValue()+"<br>");
				Window.alert(chatContent.toString());
				htmlShower.setHTML(chatContent.toString());
				Message m = new Message();
				m.setMessageID(0);
				m.setReceiver(receiver);
				m.setSender(sender);
				m.setMessageContent(htmlEditor.getValue());
				m.setIsSended(false);
				m.setSendDate(new Date());
				BeanServiceAsync<Message> service = GWT
						.create(BeanService.class);
				service.save("Message", m, new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						// TODO Auto-generated method stub
						htmlEditor.clear();
//						MessageBox.alert("message", "save success", null);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						MessageBox.alert("message", "save failure", null);
					}
				});

			}
		});
		Button cancel = new Button("取消");
		cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				htmlEditor.setValue(null);
			}
		});

		bottom.setLayout(new FitLayout());		
		bottom.addButton(send);
		bottom.addButton(cancel);
		bottom.add(panel);


		this.add(htmlShower, centerData);
		this.add(bottom, southDate);

	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public User getReceiver() {
		return receiver;
	}

	public void setReceiver(User receiver) {
		this.receiver = receiver;
	}

	public HtmlEditor getHtmlEditor() {
		return htmlEditor;
	}

	public void setHtmlEditor(HtmlEditor htmlEditor) {
		this.htmlEditor = htmlEditor;
	}

	public RichTextArea getHtmlShower() {
		return htmlShower;
	}

	public void setHtmlShower(RichTextArea htmlShower) {
		this.htmlShower = htmlShower;
	}

	public StringBuffer getChatContent() {
		return chatContent;
	}

	public void setChatContent(StringBuffer chatContent) {
		this.chatContent = chatContent;
	}
	
	

}
